<?php

namespace app\admin\controller\cdk;

use app\common\controller\AdminController;
use app\common\services\annotation\ControllerAnnotation;
use app\common\services\annotation\NodeAnnotation;

use support\Request; //请求类
use support\Response; //请求类
use think\facade\Db; //数据库类
use think\App; //初始化
use think\Exception; //异常

/**
 * @ControllerAnnotation(title="cdk_list")
 */
class ListController extends AdminController
{

    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\CdkList();
    }
    
    public function index(Request $request): Response{
        if (!$request->isAjax()) return view('admin/cdk/list/index');
        echo(456);
        if ($request->input('selectFields')) {
            return $this->selectList();
        }
        
        list($page, $limit, $where) = $this->buildTableParams();

        
        $count = $this->model->where($where)->count();
        $list  = $this->model->where($where)->order($this->order)->page($page, $limit)->select()->toArray();
        
        
        $data  = [
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list,
        ];
        return json($data);
    }
    
    public function edit(Request $request)
    {
        $id  = (int)$request->input('id');
        $row = $this->model->find($id);
        if (empty($row)) return $this->error('数据不存在');
        if ($request->isAjax()) {
            $post = $request->post();
            $lines = preg_split('/\r\n|\r|\n/', $post['content']);
            
            foreach ($lines as $line){
                $ls = explode('|', $line);
                if(count($ls) != "10"){return $this->error('代码不正确,每行要有10个参数');}
            }
            if($post['mintime']){
                $post['mintime'] = strtotime($post['mintime']);
            }
            if($post['maxtime']){
                $post['maxtime'] = strtotime($post['maxtime']);
            }
            
            try {
                $save = $row->save($post);
            } catch (\PDOException|\Exception $e) {
                return $this->error('保存失败:' . $e->getMessage());
            }
            return $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign(compact('row'));
        return $this->fetch('admin/cdk/list/edit');
    }
    
    public function add(Request $request): Response
    {
        //CDK生成方法
        function getUniqueCdk($length = 16) {
            $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $randomString = '';
        
            do {
                $randomString = '';
                for ($i = 0; $i < $length; $i++) {
                    $randomString .= $characters[rand(0, strlen($characters) - 1)];
                }
            } while (DB::name('cdk_list')->where('cdk', $randomString)->find());
        
            return $randomString;
        }
        //ADD方法
        if ($request->isAjax()) {
            $post = $request->post();
            $num = $post['num'];
            if($num == 0 or $num > 100 ){
                return $this->error('数量不正确,最大数量100');
            }
            $lines = preg_split('/\r\n|\r|\n/', $post['content']);
            
            foreach ($lines as $line){
                $ls = explode('|', $line);
                if(count($ls) != "10"){return $this->error('代码不正确,每行要有10个参数');}
            }
            $i = 0;
            while ($i < $num) {
                $arr = array();
                $arr['name'] = $post['name'].'-'.$i;
                $arr['cdk'] = getUniqueCdk();
                $arr['mintime'] = strtotime($post['mintime']);
                $arr['maxtime'] = strtotime($post['maxtime']);
                $arr['content']=$post['content'];
                $save = $this->model->insert($arr);
                $i++;
            }
            return $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch('admin/cdk/list/add');
    }
}
